package com.j.lemon.learn.asiainfo;

import com.alibaba.fastjson.JSONObject;
import com.j.lemon.learn.jdbc.DataSourceUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author lijunjun
 * @Date 2020-07-08 14:11
 * @Description
 */
public class DBTools {
    private static List<String> tables = new ArrayList<>(36);
    static {


        tables.add("uap_app_acct");
        tables.add("uap_db");
        tables.add("uap_db_acct");
        tables.add("uap_host_acct");
        tables.add("uap_host");
        tables.add("uap_main_acct");
    }
    public static void compare3AAndAudit() throws Exception {
        Connection auditConn = DataSourceUtils.getConnection("audit", "localhost:3306", "audit", "root", "123456");
        Connection threeAConn = DataSourceUtils.getConnection("3A", "localhost:3306", "3a", "root", "123456");
        Map<String,Object> result = new HashMap<>();

        for (String table : tables) {
            DatabaseMetaData metaData = threeAConn.getMetaData();
            ResultSet auditColumns = metaData.getColumns(threeAConn.getCatalog(), "audit", table, null);
            while(auditColumns.next()){
                String column_name = auditColumns.getString("COLUMN_NAME");
                String type_name = auditColumns.getString("TYPE_NAME");
                String column_size = auditColumns.getString("COLUMN_SIZE");
                DatabaseMetaData threeAConnMetaData = auditConn.getMetaData();
                ResultSet threeAColumns = threeAConnMetaData.getColumns(auditConn.getCatalog(), "audit", table, column_name);
                if(!threeAColumns.next()){
                    System.out.println("tableName:"+table+",columnName:"+column_name);
                }
//                while(threeAColumns.next()){
//                    String threeColumnName = threeAColumns.getString("COLUMN_NAME");
//                    String threeATypeName= threeAColumns.getString("TYPE_NAME");
//                    String threeAColumnSize = threeAColumns.getString("COLUMN_SIZE");
//                    if(!column_name.equals(threeColumnName) || !column_size.equals(threeAColumnSize) || !type_name.equals(threeATypeName)){
//                        Map<String,String> map = new HashMap<>();
//                        map.put("audit",column_name+","+type_name+","+column_size);
//                        map.put("3a",threeColumnName+","+threeATypeName+","+threeAColumnSize);
//                        result.put(table,map);
//                    }
//                }
            }
        }
        JSONObject jsonObject = new JSONObject(result);
        System.out.println(jsonObject.toJSONString());
    }

    public static void main(String[] args) throws Exception {
        compare3AAndAudit();
    }
}
